package com.qo.qsdk.ui.base;

import android.app.Activity;
import android.graphics.Color;
import android.text.InputType;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.qo.qsdk.model.SDKInfoModel;
import com.qo.qsdk.model.UserModel;
import com.qo.qsdk.ui.QSDKListener;
import com.qo.qsdk.utils.DynamicUtils;
import com.qo.qsdk.utils.Language;
import com.qo.qsdk.utils.QSDKHelper;

import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;

import com.qo.qsdk.ui.base.third.Px7TextView;
import com.qo.qsdk.ui.base.third.Px9TextView;
import com.qo.qsdk.ui.base.third.Px9EditText;
import com.qo.qsdk.ui.base.third.StrokeTextView;
import com.qo.qsdk.ui.base.third.StrokeTextViewY;

public class BindAView implements IBaseView{

    private Px9TextView mAccountDesc, mPwdDesc, mDesc;
    private View mPwdVisible, mAgree;

    private Px9EditText mAccountEdit, mPwdEdit;

    private StrokeTextView mBtn;
    private View mMain;
    private Activity mActivity;

    private QSDKListener mIAccountListener;

    private boolean isAgree;

    public BindAView(Activity mActivity, QSDKListener iAccountListener) {
        this.mIAccountListener = iAccountListener;
        this.mActivity = mActivity;
        init(mActivity);
    }

    private UserModel mUserModel;
    public void setUserModel(UserModel m) {
        mUserModel = m;
    }

    private void init(Activity v) {
        mMain = v.findViewById(DynamicUtils.id(mActivity, "mainBindA"));
        mMain.setVisibility(View.GONE);

        mBtn = v.findViewById(DynamicUtils.id(mActivity, "bindABtn"));
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null) {
                    mIAccountListener.onBindQBtn(mUserModel, mAccountEdit.getText().toString(), mPwdEdit.getText().toString());
                }
            }
        });
        v.findViewById(DynamicUtils.id(mActivity, "bindAClose")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIAccountListener != null) mIAccountListener.onBindQClose(mUserModel);
            }
        });

        mAccountDesc = v.findViewById(DynamicUtils.id(mActivity, "bindAAccountDesc"));
        mPwdDesc = v.findViewById(DynamicUtils.id(mActivity, "bindAPwdDesc"));

        mAccountEdit = v.findViewById(DynamicUtils.id(mActivity, "bindAAccountEdit"));
        mPwdEdit = v.findViewById(DynamicUtils.id(mActivity, "bindAPassEdit"));

        mDesc = v.findViewById(DynamicUtils.id(mActivity, "bindADesc"));
        mAgree = v.findViewById(DynamicUtils.id(mActivity, "bindAAgree"));
        mAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setActivated(!v.isActivated());
                isAgree = v.isActivated();
            }
        });
        mPwdVisible = v.findViewById(DynamicUtils.id(mActivity, "bindAPwdV"));
        mPwdVisible.setActivated(false);
        mPwdVisible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setActivated(!v.isActivated());
                if (v.isActivated()) {
                    mPwdEdit.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    mPwdEdit.setSelection(mPwdEdit.getText().length());
                }else{

                    mPwdEdit.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
                    mPwdEdit.setSelection(mPwdEdit.getText().length());
                }
            }
        });

        mMain.setVisibility(View.GONE);

        isAgree = true;
        mAgree.setActivated(isAgree);

        refresh();
    }

    public void show(Activity activity) {
        mAccountEdit.setText("");
        mPwdEdit.setText("");
        mMain.setVisibility(View.VISIBLE);
    }

    public void hide(Activity activity) {
        mMain.setVisibility(View.GONE);
    }

    public void refresh() {
        UserModel userModel = QSDKHelper.getInstance().getUserInfoModel(this.mActivity);
        SDKInfoModel config = QSDKHelper.getInstance().getConfigureModel();

        mAccountDesc.setText(Language.Bind_Account_Account.toString());
        mPwdDesc.setText(Language.Bind_Account_Pwd.toString());

        mAccountEdit.setHint(Language.Bind_Account_Tip.toString());
        mPwdEdit.setHint(Language.Bind_Pwd_Tip.toString());

        mBtn.setText(Language.Bind_Account_Btn.toString());

        String a = Language.Bind_Account_Desc_Pro.toString();
        String b = Language.Bind_Account_Desc_Pro_1.toString();
        String c = Language.Bind_Account_Desc_Pro_2.toString();
        String d = Language.Bind_Account_Desc_Pro_3.toString();
        SpannableString spannableString = new SpannableString(a);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#727785")),0, a.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#E4B575")),a.indexOf(b), a.indexOf(b) + b.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (mIAccountListener != null) {
                    mIAccountListener.onTerms();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.parseColor("#ED6E3D"));
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        },a.indexOf(b), a.indexOf(b) + b.length(), SPAN_INCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#E4B575")),  a.indexOf(d), a.indexOf(d) + d.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (mIAccountListener != null) {
                    mIAccountListener.onProtocol();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.parseColor("#ED6E3D"));
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        }, a.indexOf(d), a.indexOf(d) + d.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        mDesc.setMovementMethod(LinkMovementMethod.getInstance());
        mDesc.setText(spannableString);
        mDesc.setHighlightColor(Color.TRANSPARENT);
    }


}
